<?php


/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program.  If not, see 
 * <http://www.gnu.org/licenses/>.
*/


header('Content-type: text/xml');

// Constants used almost everywhere.
define('_JEXEC', 1);
define('JPATH_BASE', dirname(__FILE__));
define('DS', DIRECTORY_SEPARATOR);

// Required to initialize the framework.
require_once JPATH_BASE . DS . 'includes' . DS . 'defines.php';
require_once JPATH_BASE . DS . 'includes' . DS . 'framework.php';
require_once JPATH_CONFIGURATION . DS . 'configuration.php';

// Read configuration values and connect to the database server.
$conf = JFactory::getConfig();
$conf->loadObject(new JConfig());
$db = JFactory::getDBO();

// Initialize the session.
jimport('joomla.application.application');
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();


$tzd = substr(date('O'), 0, 3) . ':' . substr(date('O'), 3);
$baseUrl = JURI::base();
if (substr($baseUrl, -1) == '/')
	$baseUrl = substr($baseUrl, 0, -1);


$query = 'select concat(\'index.php?Itemid=\', i.id) loc'
	. "\n		, (select max(p.modified)"
	. "\n			from jos_content p"
	. "\n				inner join jos_sections s on s.id = p.sectionid"
	. "\n				inner join jos_categories c on c.id = p.catid"
	. "\n			where s.alias = i.alias or c.alias = i.alias) lastmod"
	. "\n		, 'weekly' changefreq"
	. "\n		, 0.5 priority"
	. "\n	from jos_menu_types m"
	. "\n		inner join jos_menu i on i.menutype = m.menutype"
	. "\n	where published = 1 order by m.menutype, i.ordering, i.lft, i.rgt"
	;
$db->setQuery($query);
$menus = $db->loadObjectList();

$query = "select concat('index.php?option=com_content&view=article&id=', p.id, ':', p.alias, '&catid=', c.id, ':'"
	. "\n		, c.alias, '&Itemid=', coalesce(m.id, 0)) loc"
	. "\n		, p.modified lastmod"
	. "\n		, 'weekly' changefreq"
	. "\n		, 0.5 priority"
	. "\n	from jos_content p"
	. "\n		inner join jos_sections s on s.id = p.sectionid"
	. "\n		inner join jos_categories c on c.id = p.catid"
	. "\n		inner join jos_menu m on m.alias = s.alias or m.alias = c.alias"
	. "\n	where now() > p.publish_up and (p.publish_down is null or now() < p.publish_down)"
	. "\n		and s.published = 1 and c.published = 1"
	. "\n	order by s.ordering, c.ordering, p.ordering"
	;
$db->setQuery($query);
$posts = $db->loadObjectList();

echo '<?xml version="1.0" encoding="utf-8"?>', "\n";


?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
	http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<?php foreach ($menus as $menu) { ?>
	<url>
		<loc><?php echo $baseUrl, JRoute::_($menu->loc, true); ?></loc>
<?php if (null !== $menu->lastmod) { ?>
		<lastmod><?php echo date('Y-m-d\TH:i:s', strtotime($menu->lastmod)), $tzd; ?></lastmod>
<?php } ?>
		<changefreq><?php echo $menu->changefreq; ?></changefreq>
		<priority><?php echo $menu->priority; ?></priority>
	</url>
<?php } ?>
<?php foreach ($posts as $post) { ?>
	<url>
		<loc><?php echo $baseUrl, JRoute::_($post->loc); ?></loc>
		<lastmod><?php echo date('Y-m-d\TH:i:s', strtotime($post->lastmod)), $tzd; ?></lastmod>
		<changefreq><?php echo $post->changefreq; ?></changefreq>
		<priority><?php echo $post->priority; ?></priority>
	</url>
<?php } ?>
</urlset>
